<template>
	<view class="main-cont">
		<view class="top box box-align-center box-pack-center">
			<view class="avar" @click="chooseImg">
				<u-avatar :src="user.icon" mode="circle" size="164"></u-avatar>
			</view>
		</view>
		<view class="info">
			<u-form :model="user" ref="uForm" :rules="rules" label-width="140">
				<u-form-item label="姓名" prop="name">
					<u-input v-model="user.name" />
				</u-form-item>
				<u-form-item label="职工号">
					<view class="">
						{{user.zgh!=null?user.zgh:'暂无职工号'}}
					</view>
				</u-form-item>
				<u-form-item label="身份">
					<view class="">
						老师
					</view>
				</u-form-item>
				<u-form-item label="学校">
					<view class="">
						{{user.schoolName}}
					</view>
				</u-form-item>
				<u-form-item label="手机号">
					<view class="">
						{{user.mobile}}
					</view>
				</u-form-item>
			</u-form>
			<view class="box box-pack-center box-align-center">
				<u-button size="medium" @click="save">保存</u-button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: uni.getStorageSync("user"),
				/* roleList:uni.getStorageSync("roleList"), */
				phone: '',
				customStyle: {
					background: "",
					padding: '0'
				},
				name: uni.getStorageSync("user").name,
				form: {
					name: '',
					xh: '',
					mobile: '',
					schoolName: ''
				},
				rules: {
					name: [{
						required: true,
						message: '姓名不能为空',
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		onLoad(opt) {
			this.user = uni.getStorageSync('user')
			//console.log(this.user);
			// this.setData()

		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			//获取数据
			setData() {
				this.form = {
					name: this.user.name,
					xh: this.user.xh != null ? this.user.xh : '',
					mobile: this.user.mobile,
					schoolName: this.user.schoolName
				}
			},
			//获取手机号
			getPhoneNumber(e) {
				uni.login({
					provider: 'weixin',
					success: (res) => {

						let opts = {
							url: '/comm/getPhoneFromApplet',
							method: 'post'
						};
						let param = {
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
							code: res.code
						};

						this.$api.post(opts, param).then(resp => {
							//console.log(resp);
							this.user.mobile = resp.phoneNumber;
							let params = {
								id: this.user.id,
								mobile: resp.phoneNumber
							};
							this.updateUser(params);
						})
					}
				})
			},
			chooseImg() {
				let _this = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function(res) {
						_this.uploadfile(res.tempFilePaths[0]);
						_this.$nextTick((resp => {
							_this.user.icon = res.tempFilePaths[0]
						}))
					}
				});
			},
			uploadfile(file) {
				var _this = this;
				// //console.log(_this.$api.baseUrl)
				uni.uploadFile({
					url: _this.$api.baseUrl + '/comm/uploadfile',
					filePath: file,
					name: 'file',
					success: (uploadFileRes) => {
						const resdata = JSON.parse(uploadFileRes.data);
						_this.user.icon = resdata.url;
						// let params = {
						// 	id: _this.user.id,
						// 	icon: resdata.url
						// };
						// _this.updateUser(params);
					}
				});
			},
			updateUser(params) {
				let opts = {
					url: '/sysuser/modify',
					method: 'post'
				};
				this.$api.post(opts, params).then(res => {
					uni.setStorageSync("user", this.user);
				})
			},
			save() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						//console.log('验证通过');
						let opts = {
							url: '/sysuser/modify',
							method: 'post'
						};
						let params = {
							name: this.user.name,
							icon: this.user.icon,
							xh: this.user.xh,
							id: this.user.id
						}
						this.$api.post(opts, params).then(res => {
							if (res.code == 0) {
								uni.showToast({
									title: '保存成功',
									icon: 'none'
								})
							}
							uni.setStorageSync("user", this.user);

						})

					} else {
						//console.log('验证失败');
					}
				});
			},

		}
	}
</script>

<style scoped lang="scss">
	.main-cont {

		.top {
			height: 236rpx;
		}

		.info {
			padding: 20px;
			margin: 0 14rpx;
			width: 722rpx;
			// height: 403rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
		}

		.phone {
			margin: 40rpx 14rpx;
			width: 722rpx;
			height: 100rpx;
			background: #FFFFFF;
			border-radius: 8rpx;

			.list {
				width: 100%;
			}
		}

		.list {
			padding: 0 48rpx;
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #F0F1F8;

			&:last-child {
				border: none;
			}
		}
	}
</style>
